package com.ckm.creator;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.data.mongodb.MongoDatabaseFactory;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class DefaultMongoSourceCreator {

    private final List<MongoSourceCreator> creators;

    /**
     * 创建数据源
     * @param mongoProperties
     * @return
     */
    public MongoDatabaseFactory createMongoDataSource(MongoProperties mongoProperties){
        MongoSourceCreator mongoSourceCreator = null;
        for (MongoSourceCreator creator : creators) {
            mongoSourceCreator = creator;
            break;
        }
        if (mongoSourceCreator == null){
            throw new RuntimeException("creator 不能为空，请检查MongoSourceCreator");
        }

        MongoDatabaseFactory mongoSource = mongoSourceCreator.createMongoSource(mongoProperties);

        return mongoSource;
    }
}
